package cm.cool.system.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;


import cm.cool.web.util.cookie.CookieOp;
import cm.cool.web.util.respon.R;
import cm.cool.web.util.tokencache.TokenUtil;

/**
 * 使用注解标注过滤器
 * @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器
 * 属性filterName声明过滤器的名称,可选
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
 * 
 */
@WebFilter(filterName="myFilter",urlPatterns="/*")
public class ResponFilter implements Filter {
	
	//超时时间
	@Value("${token.outTime}")
	private String outTime;
	
	//cookie名称
	@Value("${cookie.name}")
	private String cookieName;
	
	//免拦截地址
	@Value("${not.intercept}")
	private String notIntercept;
	
	static List<String> uriList=new ArrayList<String>();
	
    @Override
    public void destroy() {
        System.out.println("过滤器销毁");
    }
    
    /**
     * 拦截器
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest) request;
        HttpServletResponse httpServletResponse=(HttpServletResponse) response;
    	
    	 
//        String requestPath=httpServletRequest.getRequestURI(); 
//        System.out.println(httpServletRequest.getRequestURI());
//        //不过滤静态资源
//        String res = requestPath.substring(requestPath.lastIndexOf(".") + 1).toLowerCase(); 
//        if(res.equals("js") || res.equals("css") || res.equals("html") || res.equals("jpg") || res.equals("png")){
//        	chain.doFilter(request, response);
//        	return;
//        }
//        
//    	for(int i=uriList.size()-1;i>=0;i--){
//    		if(uriList.get(i).equals(requestPath)){
//    			 chain.doFilter(request, response);
//    			 return;
//    		}
//    		
//    	}
//    	
//    	//判断cookie是否正确获取
//    	if(R.isNull(cookieName)){
//    		cookieName="securityName";
//    	}
//    	
//    	//拿到token的值
//       String tokenValue=CookieOp.getCookie(httpServletRequest, cookieName);
//       
//       //判断token是否为空
//       if(R.isNull(tokenValue)){
//    	   System.out.println("token为空");
//    	   httpServletResponse.sendRedirect("/login.html");
//    	   return;
//       }
//        
//       //验证Token信息是否合法
//       if(!TokenUtil.checkToken(tokenValue,outTime)){
//    	   System.out.println("token不合法");
//    	   httpServletResponse.sendRedirect("/login.html");
//    	   return;
//       }
       
       
        chain.doFilter(request, response);
    }
    
    /**
     * 初始化拦截路径
     */
    @Override
    public void init(FilterConfig config) throws ServletException {
    	if(R.isNull(notIntercept)){
    		uriList.add("/login/login");
    		return;
    	}
    	String[] st=notIntercept.split(",");
    	uriList=Arrays.asList(st);
    }
    
    
}